package com.yuchting.dinneronline.client.common;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;


public class CommonDialog {
	
	public static final KeyPressHandler 	fsm_socketPortHandler = new KeyPressHandler() {
		
		public void onKeyPress(KeyPressEvent event) {
			TextBox t_box = (TextBox) event.getSource();
			if(!Character.isDigit(event.getCharCode())) {
				t_box.cancelKey();
		    }
			
			final int t_maxPort = 65535;
			if(Integer.valueOf(t_box.getText() + event.getCharCode()).intValue() > t_maxPort){
				t_box.cancelKey();
				t_box.setText(Integer.toString(t_maxPort));
			}
		}
	};
	
	private static class waitingLabel extends PopupPanel{

		final HTML		m_label = new HTML();
		
	    public waitingLabel() {
			// PopupPanel's constructor takes 'auto-hide' as its boolean parameter.
			// If this is set, the panel closes itself automatically when the user
			// clicks outside of it.
			super();
			
			final HorizontalPanel t_horz = new HorizontalPanel();
			t_horz.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			Image t_waiting = new Image("logon.gif");
			t_horz.add(t_waiting);
			t_horz.add(m_label);
			
			m_label.setWordWrap(true);
			
			setWidget(t_horz);
			  
			// waiting label
			setStyleName("waitingLabel");	    
	    }
	    
	    public void ShowText(String _text){
	    	
	    	_text = _text.replace("\n", "<br />");    	
	    	m_label.setHTML(_text);	    
	    		
	    	setGlassEnabled(true);
	    	setModal(true);
	    	center();
	    	show();	
	    }
	    
	    public void Hide(){
	    	setModal(false);
	    	hide();	    	
	    }
	}

	static private final DecoratedPopupPanel fsm_simplePopup = new DecoratedPopupPanel(true);
	  
	// waiting label
	public static final waitingLabel			fsm_waitingLable		= new waitingLabel();
	static YesNoDialog		sm_yesNoDlg = null;
	
	
	public static void PopupPrompt(String _prompt,Exception e,Widget _attachWidget){
		PopupPrompt(_prompt + " :" + e.getMessage() + " " + e.getClass().getName(),_attachWidget);
	}
	
	public static void PopupPrompt(String _prompt,Widget _attachWidget){

		_prompt = _prompt.replaceAll("[\r\n]", "<br />");
		
		HTML label = new HTML(_prompt);
		label.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		
		fsm_simplePopup.setWidget(label);
		
		int t_maxLine = 0;
		String[] t_lines =_prompt.split("<br />");
		
		for(String line:t_lines){
			// get ride of HTML tag
			//
			if(line.indexOf("/>") == -1 && line.indexOf("</") == -1
					
			&& t_maxLine < line.length()){
				t_maxLine = line.length();
			}
		}
		
		if(_attachWidget == null){
			 fsm_simplePopup.center();
		}else{
			int left;
			int top;
			
			if(_attachWidget == null){
				left = 100;
		        top = 300;
			}else{
				left = _attachWidget.getAbsoluteLeft() + (_attachWidget.getOffsetWidth() - t_maxLine * 13) / 2;
		        top = _attachWidget.getAbsoluteTop() + (_attachWidget.getOffsetHeight()) / 2;	
			}
			
			if(left < 0){
				left = 0;
			}
			
	        fsm_simplePopup.setPopupPosition(left, top);	
		}
		
        
        // Show the popup
        fsm_simplePopup.show();      
	}
		
	public static void PopupYesNoDlg(String _prompt , YesNoHandler _yes,YesNoHandler _no){
		if(sm_yesNoDlg == null){
			sm_yesNoDlg = new YesNoDialog();
		}
		
		sm_yesNoDlg.Popup(_prompt, _yes, _no);
	}
	
	public static void PopupWaiting(String _prompt){
		if(_prompt != null && _prompt.length() != 0){
			fsm_waitingLable.ShowText(_prompt);
		}
	}
	
	public static void HideWaiting(){
		fsm_waitingLable.Hide();
	}
	
	public static void PopupDialogPrompt(String _prompt){
		
		_prompt = _prompt.replaceAll("[\r\n]", "<br />");
		
		final DialogBox t_dlg = new DialogBox();
		VerticalPanel t_main = new VerticalPanel();
		t_main.add(new HTML(_prompt + "<br />"));
		
		Button t_button = new Button("我知道了！");
		t_main.add(t_button);
		t_button.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				t_dlg.hide();
			}
		});
		
		t_dlg.setWidget(t_main);
		t_dlg.setText("提示");
		
		t_dlg.setAnimationEnabled(true);
		t_dlg.setGlassEnabled(true);	
		
		t_dlg.center();
		t_dlg.show();
	}
	
	public static void setOpacity(com.google.gwt.dom.client.Element _elm,float _opacity){
		com.google.gwt.dom.client.Style t_style = _elm.getStyle();
		
		if(_opacity >= 1.0f){
			t_style.setProperty("fliter","alpha(opacity=100)");
			t_style.setProperty("opacity", "1");
		}else if(_opacity <= 0){
			t_style.setProperty("fliter","alpha(opacity=0)");
			t_style.setProperty("opacity", "0");
		}else{
			String s = Integer.toString((int)Math.round(_opacity * 100));
			String sDecimal = (s.length() == 1 ? ".0" : ".") + s;
			
			t_style.setProperty("fliter","alpha(opacity="+s+")");
			t_style.setProperty("opacity", sDecimal);
		}
	}
	
	public static void AddLabelWidget(Panel _pane,final String _label,final Widget _widget){
		_pane.add(new Label(_label));
		_pane.add(_widget);
	}
	
	public static void AddLabelWidget(FlexTable _table,final String _label,final Widget _widget,int _line){
		_table.setWidget(_line, 0, new HTML(_label));
		_table.setWidget(_line, 1, _widget);
	}
	
	public static boolean isValidPassword(String _password){
		
		if(_password.length() < 6){
			return false;
		}
		
		int t_index = 0;
		while(t_index < _password.length()){
			if(Character.isLetterOrDigit(_password.charAt(t_index)) == false){
				return false;
			}
			t_index++;
		}
		
		return true;
	}
	
	public native static boolean isValidEmail(String name)/*-{
	    var regex = new RegExp("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
	    return regex.test(name);
	}-*/;
	
	public static native String getElementWidth(com.google.gwt.dom.client.Element _elm)/*-{
		if(window.getComputedStyle){
			
			return window.getComputedStyle(_elm,"").width;
		  
		} else if( _elm.currentStyle ){
			// IE 
			//
			return _elm.currentStyle.width;
		}
		
		return null;
	}-*/;
		
	public native static boolean isValidIP(String ip)/*-{
		var regex = new RegExp("^((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))$");
	    return regex.test(ip);
	}-*/;

}
